const { imageBaseUrl } = require('../api/url')
const cityData = require('./cityData')
/**
 * @method
 * @desc 获取n位随机数
 */
function getRandomNum(n) {
  let num = ''
  for (let i = 0; i < n; i += 1) {
    num += Math.floor(Math.random() * 10)
  }
  return +num
}
/**
 * @method 
 * @desc 格式化支付时间
 */
function formatOrderPayTime(timeNum) {
  const d = new Date(timeNum)
  return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())} ${pad(d.getHours())}:${pad(d.getMinutes())}:${pad(d.getSeconds())}`
}
/**
 * @method
 * @desc 格式化时间
 * @param datae Number 毫秒数
 */
const formatTime = date => {
  date = new Date(date)
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

function pad(num) {
  if (num < 10) return '0' + num
  return num
}
//格式化订单创建时间
function formatOrderTime(timeNum) {
  const d = new Date(timeNum)
  return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())} ${pad(d.getHours())}:${pad(d.getMinutes())}:${pad(d.getSeconds())}`
}
//格式化地址信息
function formatRegion(fProvinceNum, fCityNum, fAreaNum) {
  var province = null,
    city = null,
    area = null,
    provinceName = null,
    cityName = null,
    areaName = null,
    location = null
  for (var i = 0; i < cityData.length; i++) {
    if (cityData[i].id == fProvinceNum) {
      province = cityData[i].sub
      provinceName = cityData[i].name
    }
  }
  for (var j = 0; j < province.length; j++) {
    if (province[j].id == fCityNum) {
      city = province[j].sub
      cityName = province[j].name
    }
  }
  if (city && city.length) {
    for (var i = 0; i < city.length; i++) {
      if (city[i].id == fAreaNum) {
        areaName = city[i].name
        location = cityName + '  ' + areaName
      }
    }
  } else {
    areaName = '',
      location = provinceName + '  ' + cityName
  }
  return {
    region: provinceName + '  ' + cityName + '  ' + areaName,
    location: provinceName + '  ' + location,
  }
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}
/**
 * @method
 * @desc 格式化sku
 * @param {Array} data goods.skuAndPropAndPropValList
 */
function formatSku(data, goodsId) {
  if (!data || !data.length) { //如果有一个为空，则返回
    return {
      skuList: [],
      priceMap: []
    }
  }
  const skuListData = data.map(i => i.skuPropIdAndNameAndValIdAndNameList)
  const skuList = []
  for (let i = 0; i < skuListData[0].length; i += 1) {
    const skuData = skuListData.map(item => item[i])

    const options = []
    for (let j = 0; j < skuData.length; j += 1) {
      const s = skuData[j];
      if (!options.find(option => option.name === s.fPropValName)) {
        options.push({
          img: s.fImage && imageBaseUrl + s.fImage,
          name: s.fPropValName,
          selected: false,
        })
      }
    }
    if (options && options.length) {
      options[0].selected = true
    }
    skuList.push({
      name: skuData[0].fPropName,
      options,
    })
  }
  const priceMap = data.map(i => ({
    goodsId,
    id: i.fId,
    map: i.skuPropIdAndNameAndValIdAndNameList.map(j => j.fPropValName),
    price: i.fPrice.toFixed(2),
    stock: i.fStock,
    stockPrice: i.fProductionCost,
    dealerCode: i.fBandCode,
  }))

  return {
    skuList,
    priceMap
  }
}
/**
 * @method
 * @desc 提取城市名称
 * @param 
 */
function formatAddrCodeToString(provinceNum, cityNum, areaNum) {
  let province,city,area
  if (provinceNum&&cityData) {
    province = cityData.find(val => val.id === provinceNum)
  }
  if (province && province.sub && province.sub.length > 0) {
    let temProSub = province.sub
    city = temProSub.find(val1 => val1.id === cityNum)
  }
  if(city&&city.sub&&city.sub.length>0){
    let temCitySub=city.sub
    area =temCitySub.find(val2=>val2.id===areaNum)
  }
  return ((province ? province.name : '')+ (city ?( '-'+city.name) : '')+ (area ? ('-'+area.name) : ''))
}

module.exports = {
  formatTime: formatTime,
  formatOrderTime: formatOrderTime,
  formatRegion: formatRegion,
  formatSku: formatSku,
  formatAddrCodeToString,
  formatOrderPayTime,
  getRandomNum
}
